package com.infindo.iflower;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
import com.infindo.common.Constant;
import com.infindo.iflower.util.AssetsUtil;
import com.infindo.iflower.util.FileUtil;

public class EmailWrite extends Activity{
	private String[] picnames;
	private int picIndex;
	private EditText cctext ;
	private EditText totext;
	private EditText subjecttext;
	private String prebtntype;
	private Activity mActivity;
	String picname;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.email_write);
		mActivity = this;
		Intent preintent = this.getIntent();
		Bundle bundle = preintent.getExtras();
		picnames = bundle.getStringArray("picNames");
		picIndex = bundle.getInt("picIndex");
		prebtntype = bundle.getString(Constant.ClASS_METHOD); //if favorite button
		
		cctext = (EditText)findViewById(R.id.emailccid);
		totext = (EditText)findViewById(R.id.emailtoid);
		subjecttext = (EditText)findViewById(R.id.emailsubjectid);
		picname = picnames[picIndex];
		System.out.println("...............picname = "+picname);
		System.out.println("..................prebtntype..."+prebtntype);
//		Button sendBtn = (Button)findViewById(R.id.sendid);
		
		String ccstr = cctext.toString();
		String tostr = totext.toString();
		String substr = subjecttext.toString();
		System.out.println("..................prebtntype..."+prebtntype);
		if(Constant.FILENAME_FROM_GALLERY.equals(prebtntype)){
			addtoSDtemp(picname);
			
			File file = new File(Constant.FAV_PATH_TEMP+picname);
			picname = file.getAbsolutePath();
		}
		
		sendEmail(ccstr,tostr,substr,picname);
		
		
	}
	
	private void sendEmail(String ccstr,String tostr,String substr,String stream){  
		Intent it = new Intent(Intent.ACTION_SEND);       
//		it.putExtra(Intent.EXTRA_EMAIL, tostr);   
//		it.putExtra(Intent.EXTRA_CC, ccstr);  
		it.putExtra(Intent.EXTRA_SUBJECT, substr); 
		//attach   
		it.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+stream));   
//		it.setType("message/rfc822"); 
		it.setType("image/*");  
//		it.setType("plain/text");
		
//		startActivity(Intent.createChooser(it, "Choose, Email Client"));   
		startActivity(it);
		finish();
		
	}
	
	private void addtoSDtemp(String picName){
		if (FileUtil.isSdPresent()) {
			if (AssetsUtil.saveAssetsToSdcard(mActivity, AssetsUtil.PICTURE_PATH_BIG + "/",
					picName, Constant.FAV_PATH_TEMP)) {
			} else {
				// failure
				Toast.makeText(mActivity, R.string.fav_add_failure,
						Toast.LENGTH_SHORT).show();
			}

		} else {
			Toast.makeText(mActivity, R.string.fav_no_sdcard,
					Toast.LENGTH_SHORT).show();
		}
	}
}
